import random
import time

import math
import pywasm
import requests

from codes.commons.configs import easiest_common_headers as common_headers


def get_m():
    t = int(time.time())
    t1 = int(t / 2)
    t2 = int(t / 2 - math.floor(random.random() * 50 + 1))
    wasm = pywasm.load('../../resources/match15/main.wasm')
    sign = wasm.exec('encode', [t1, t2])
    return f"{sign}|{t1}|{t2}"


def request_pages():
    # url = 'http://match.yuanrenxue.com/api/match/15?m=17738731%7C807371933%7C807371926&page=1'
    url = 'http://match.yuanrenxue.com/api/match/15'
    nums_sum = []
    for p in range(1, 6):
        params = {
            'page': p,
            'm': get_m()
        }
        res_json = requests.get(url=url, params=params, headers=common_headers).json()
        print(f'获取第【{p}】页数据：{res_json}')
        data = [tt['value'] for tt in res_json['data']]
        nums_sum.extend(data)
    print(f'总计:{sum(nums_sum)}')


if __name__ == '__main__':
    request_pages()
